<?php
/**
 * Erstellt und verwaltet Kategoriestrukturierungen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Category_StructureList
{
    /** @var $oSql Yapary_Database_Sql */
    private $oSql           = NULL;
    /** @var $dbTableName string */
    private $dbTableName    = 'yapary_category_structure';


    /**
     * @param Object $_oSql	: Datenbankklasse fuer Datenbankzugriff
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql)
    {
        $this->oSql  = $_oSql;
    }


    /**
     * Fuegt einen neuen Datensatz hinzu
     * @return Yapary_Category_Structure|NULL
     * */
    public function add ($name1, $name2 = '', $name3 = '', $name4 = '', $name5 = '')
    {
        $oDbResult = $this->oSql->request('INSERT INTO '.$this->dbTableName.'(Name1, Name2, Name3, Name4, Name5) VALUES (:name1, :name2, :name3, :name4, :name5)',
                                 array('name1' => $name1,
                                       'name2' => $name2,
                                       'name3' => $name3,
                                       'name4' => $name4,
                                       'name5' => $name5));
        $lastId = mysql_insert_id();
        return new Yapary_Category_Structure($this->oSql, $lastId);
    }


    /**
     * Loescht einen existierenden Datensatz
     * @return bool
     * */
    public function delete ($id)
    {
        $oDbResult = $this->oSql->request('DELETE
        					  FROM '.$this->dbTableName.'
        					  WHERE Id=:id',
                            array('id' => $id));
        return true;
    }


    /**
     * Liefert einen Datensatz
     * @return Yapary_Category_Structure
     * */
    public function get ($_id)
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Id=:id',
                            array('id' => $_id));

        if ($oDbResult->getCount() == 1) {
            $row = $oDbResult->getNext();
            return new Yapary_Category_Structure($this->oSql, $row);
        }

        return NULL;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Category_Structure]
     * */
    public function getList1 ($orderBy = 'Prio')
    {
        $arrResult = array();
        $orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $orderBy)) ? ' ORDER BY '.$orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name2=""
        					  '.$orderBy.'',
                            array());

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Category_Structure($this->oSql, $row);
            }
        }

        return $arrResult;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Category_Structure]
     * */
    public function getList2 ($name1, $orderBy = 'Prio')
    {
        $arrResult = array();
        $orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $orderBy)) ? ' ORDER BY '.$orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name1=:name1 AND
        					  		Name2!="" AND
        					  		Name3=""
        					  '.$orderBy.'',
                            array('name1' => $name1));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Category_Structure($this->oSql, $row);
            }
        }

        return $arrResult;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Category_Structure]
     * */
    public function getList3 ($name1, $name2, $orderBy = 'Prio')
    {
        $arrResult = array();
        $orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $orderBy)) ? ' ORDER BY '.$orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name1=:name1 AND
        					  		Name2=:name2 AND
        					  		Name3!="" AND
        					  		Name4=""
        					  '.$orderBy.'',
                            array('name1' => $name1,
                                  'name2' => $name2));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Category_Structure($this->oSql, $row);
            }
        }

        return $arrResult;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Category_Structure]
     * */
    public function getList4 ($name1, $name2, $name3, $orderBy = 'Prio')
    {
        $arrResult = array();
        $orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $orderBy)) ? ' ORDER BY '.$orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name1=:name1 AND
        					  		Name2=:name2 AND
        					  		Name3=:name3 AND
        					  		Name4!="" AND
        					  		Name5=""
        					  '.$orderBy.'',
                            array('name1' => $name1,
                                  'name2' => $name2,
                                  'name3' => $name3));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Category_Structure($this->oSql, $row);
            }
        }

        return $arrResult;
    }


    /**
     * Liefert einen Datensatz
     * @return Array[Yapary_Category_Structure]
     * */
    public function getList5 ($name1, $name2, $name3, $name4, $orderBy = 'Prio')
    {
        $arrResult = array();
        $orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $orderBy)) ? ' ORDER BY '.$orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name1=:name1 AND
        					  		Name2=:name2 AND
        					  		Name3=:name3 AND
        					  		Name4=:name4 AND
        					  		Name5!=""
        					  '.$orderBy.'',
                            array('name1' => $name1,
                                  'name2' => $name2,
                                  'name3' => $name3,
                                  'name4' => $name4));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_Category_Structure($this->oSql, $row);
            }
        }

        return $arrResult;
    }


}


?>